<?php
namespace Realisation\Web\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Realisation\Web\Builder;

/**
 * Description of AbstractApiController
 *
 * @author pahhan
 */
abstract class AbstractApiController
{
    /**
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     * @throws \Exception
     */
    protected function createResponse(Request $request)
    {
        $format = $request->get('format', 'xml');

        switch ($format) {
            case 'xml':
                 $response = new Response();
                 $response->headers->set('content-type', 'text/xml');
                break;

            case 'json':
                $response = new Response();
                $response->headers->set('content-type', 'application/json');
               break;

            default:
                throw new \Exception('Unknown format type '.$format);
        }

        return $response;
    }

    /**
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     * @return \Realisation\Web\Builder\BuilderInterface
     * @throws \Exception
     */
    protected function createBuilder(Request $request)
    {
        $format = $request->get('format', 'xml');

        switch ($format) {
            case 'xml':
                return new Builder\XmlBuilder();
                break;

            case 'json':
                return new Builder\JsonBuilder();
                break;

            default:
                throw new \Exception('Unknown format type '.$format);
        }
    }
}